﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
using SDLITS.Common;

namespace SDLITS.Web.Driver
{
    public partial class CameraPhoto : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                if(!CacheHelper.ExistIdentify("Id"))
                CacheHelper.InsertIdentify("Id", Request["id"], 60, 0);
                if (!CacheHelper.ExistIdentify("ImgName"))
                CacheHelper.InsertIdentify("ImgName",DateTime.Now.ToString("yyyyMMddHHmmss"), 60, 0);
            }
           // Response.Write(SDLITS.Common.CacheHelper.ReadIdentify("CamerIsTrue"));
            if (Request.Form["PHeight"] != null && Request.Form["PWidth"] != null && Request.Form["strBMP"] != null)
            {
                try
                {
                    Response.Write("PWidth：" + Request.Form["PWidth"].ToString() + "<br/>");
                    Response.Write("PHeight：" + Request.Form["PHeight"].ToString() + "<br/>");

                    int height = int.Parse(Request.Form["PHeight"].ToString());
                    int width = int.Parse(Request.Form["PWidth"].ToString());

                    string strBmp = Request.Form["strBMP"].ToString();
                    Response.Write(strBmp + "<br/>");

                    SaveBmp(BuildBitmap(width, height, strBmp));

                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }
        public System.Drawing.Bitmap BuildBitmap(int width, int height, string strBmp)
        {
            System.Drawing.Bitmap tmpBmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            string[] StmpBmp = strBmp.Split(',');
            int pos = 0;
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    tmpBmp.SetPixel(x, y, Color.FromArgb(int.Parse(StmpBmp[pos], System.Globalization.NumberStyles.HexNumber)));
                    pos++;
                }
            }
            return tmpBmp;
        }
        public void SaveBmp(System.Drawing.Bitmap bmp)
        {
            string FileName = Server.MapPath("image") + "\\" + System.Guid.NewGuid().ToString() + ".bmp";
            bmp.Save(FileName, System.Drawing.Imaging.ImageFormat.Bmp);
        }

        protected void timer1_Tick(object sender, EventArgs e)
        {
            if (SDLITS.Common.CacheHelper.ExistIdentify("CamerIsTrue"))
            {
                if (SDLITS.Common.CacheHelper.ReadIdentify("CamerIsTrue").ToString() == "1")
                {
                    SDLITS.Common.CacheHelper.RemoveIdentify("CamerIsTrue");
                    FineUI.PageContext.RegisterStartupScript(FineUI.ActiveWindow.GetHidePostBackReference());
                    //FineUI.Alert.Show("1");
                }
                //   
                //FineUI.Alert.Show("1");
            }
        }
    }
}